﻿using System;
using fastCSharp;

namespace diantou.web.contest
{
    /// <summary>
    /// 题目
    /// </summary>
    /// <typeparam name="viewType"></typeparam>
    abstract class problemView<viewType> : web.view<viewType>
        where viewType : problemView<viewType>
    {
        /// <summary>
        /// 问题
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected diantou.dataAccess.problem problem;
        /// <summary>
        /// 用户OJ问题
        /// </summary>
        protected web.onlineJudge.userProblem userProblem
        {
            get { return new onlineJudge.userProblem { Problem = problem, User = currentUser }; }
        }
        /// <summary>
        /// 比赛
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected diantou.dataAccess.contest contest;
        /// <summary>
        /// 用户比赛关系
        /// </summary>
        protected userContest userContest
        {
            get { return new userContest { Contest = contest, User = currentUser }; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="problemId"></param>
        /// <returns></returns>
        protected virtual bool loadView(int problemId)
        {
            if (problemId == 0)
            {
                checkSearchEngine(diantou.dataModel.path.pub.Default.ContestListPath, webConfig.NotFound404);
                return false;
            }
            problem = diantou.dataAccess.clientCache.Problem[problemId];
            if (problem != null)
            {
                int point = diantou.dataAccess.clientCache.ContestProblem[new diantou.dataModel.contestProblem.primaryKey { ContestId = problem.ContestId, ProblemId = problem.Id }];
                if (point >= 0)
                {
                    contest = diantou.dataAccess.clientCache.Contest[problem.ContestId];
                    if (contest != null)
                    {
                        if (problem.BasePoint != point) (problem = problem.Clone()).BasePoint = point;
                        if (contest.IsShow && date.Now >= contest.StartTime) return true;
                        if (currentUser != null && (currentUser.Permission == dataModel.user.permission.Admin || problem.UserId == currentUser.Id || problem.TestUserId == currentUser.Id))
                        {
                            return true;
                        }
                    }
                }
                problem = null;
            }
            AjaxResponse(webConfig.NotFound404);
            return false;
        }
    }
}
